/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MENU_EDIT.java
 *
 * Created on Oct 5, 2010, 9:17:02 PM
 */

package cat200project;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author akmal
 */
public class MENU_EDIT extends javax.swing.JFrame {

    private String url = "jdbc:oracle:thin:@localhost:1521:XE";
    private String pass_id = "restaurant";
    private String pass_pw = "restaurant";
    String newline = "\n";
    DefaultListModel List;
    DefaultListModel TempList;
    Connection con;
    ResultSet rset;
    Statement stat;
    boolean edit = false;
    boolean add = false;

    /** Creates new form MENU_EDIT */
    public MENU_EDIT() throws SQLException {
        super("MENU PROFILE");
        initComponents();
        this.setLocationRelativeTo(null);
        connect();
        populateData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        FoodList = new javax.swing.JList();
        DoneButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        resetButton = new javax.swing.JButton();
        FoodTypeCombo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        FoodCode = new javax.swing.JTextField();
        FoodType = new javax.swing.JTextField();
        FoodName = new javax.swing.JTextField();
        FoodPrice = new javax.swing.JTextField();
        jLabel23 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jSeparator1.setBounds(0, 50, 700, 10);
        jLayeredPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator2.setBounds(0, 420, 700, 20);
        jLayeredPane1.add(jSeparator2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        FoodList.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        FoodList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        FoodList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                FoodListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(FoodList);

        jScrollPane1.setBounds(40, 100, 130, 280);
        jLayeredPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        DoneButton.setFont(new java.awt.Font("Times New Roman", 1, 12));
        DoneButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        DoneButton.setText("DONE");
        DoneButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        DoneButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DoneButtonActionPerformed(evt);
            }
        });
        DoneButton.setBounds(580, 430, 100, 40);
        jLayeredPane1.add(DoneButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        addButton.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        addButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        addButton.setText("ADD");
        addButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });
        addButton.setBounds(250, 330, 90, 40);
        jLayeredPane1.add(addButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        editButton.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        editButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        editButton.setText("EDIT");
        editButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });
        editButton.setBounds(350, 330, 90, 40);
        jLayeredPane1.add(editButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        deleteButton.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        deleteButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        deleteButton.setText("DELETE");
        deleteButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });
        deleteButton.setBounds(450, 330, 90, 40);
        jLayeredPane1.add(deleteButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        saveButton.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        saveButton.setText("SAVE");
        saveButton.setEnabled(false);
        saveButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });
        saveButton.setBounds(550, 330, 90, 40);
        jLayeredPane1.add(saveButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        resetButton.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        resetButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        resetButton.setText("Reset");
        resetButton.setEnabled(false);
        resetButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        resetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetButtonActionPerformed(evt);
            }
        });
        resetButton.setBounds(560, 230, 90, 40);
        jLayeredPane1.add(resetButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        FoodTypeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "Appetizer", "Main Dish", "Dessert", "Drink" }));
        FoodTypeCombo.setEnabled(false);
        FoodTypeCombo.setBounds(530, 150, 130, 20);
        jLayeredPane1.add(FoodTypeCombo, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel1.setText("MENU PROFILE");
        jLabel1.setBounds(40, 10, 310, 50);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel2.setText("Food Code");
        jLabel2.setBounds(220, 110, 90, 20);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel3.setText("Food Type");
        jLabel3.setBounds(220, 150, 100, 17);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel4.setText("Food Name");
        jLabel4.setBounds(220, 190, 100, 17);
        jLayeredPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel5.setText("Food Price");
        jLabel5.setBounds(220, 230, 110, 17);
        jLayeredPane1.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jLabel6.setText("RM");
        jLabel6.setBounds(360, 230, 19, 20);
        jLayeredPane1.add(jLabel6, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel7.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Available Food", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Times New Roman", 1, 12))); // NOI18N
        jLabel7.setBounds(30, 70, 150, 330);
        jLayeredPane1.add(jLabel7, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel8.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Menu Info", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Times New Roman", 1, 12))); // NOI18N
        jLabel8.setBounds(190, 70, 490, 340);
        jLayeredPane1.add(jLabel8, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel9.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel9.setBounds(230, 310, 430, 80);
        jLayeredPane1.add(jLabel9, javax.swing.JLayeredPane.DEFAULT_LAYER);

        FoodCode.setFocusable(false);
        FoodCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FoodCodeActionPerformed(evt);
            }
        });
        FoodCode.setBounds(360, 110, 150, 20);
        jLayeredPane1.add(FoodCode, javax.swing.JLayeredPane.DEFAULT_LAYER);

        FoodType.setFocusable(false);
        FoodType.setBounds(360, 150, 150, 20);
        jLayeredPane1.add(FoodType, javax.swing.JLayeredPane.DEFAULT_LAYER);

        FoodName.setFocusable(false);
        FoodName.setBounds(360, 190, 150, 20);
        jLayeredPane1.add(FoodName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        FoodPrice.setFocusable(false);
        FoodPrice.setBounds(390, 230, 120, 20);
        jLayeredPane1.add(FoodPrice, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel23.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract2.jpg"))); // NOI18N
        jLabel23.setText("jLabel23");
        jLabel23.setBounds(0, 0, 700, 500);
        jLayeredPane1.add(jLabel23, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 700, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void DoneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DoneButtonActionPerformed

        this.setVisible(false);
        new EMPLOYEE_MENU().setVisible(true);
        // TODO add your handling code here:
    }//GEN-LAST:event_DoneButtonActionPerformed

    private void FoodCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FoodCodeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_FoodCodeActionPerformed

    private void FoodListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_FoodListValueChanged

        String select = (String) FoodList.getSelectedValue();
        String query;

        query = "SELECT * FROM FOOD WHERE FOOD_NAME = '"+select+"'";

        try
        {
            rset = getResultSet(query);
            while(rset.next())
            {
                FoodCode.setText(rset.getString("FOOD_CODE"));
                FoodType.setText(rset.getString("FOOD_TYPE"));
                FoodName.setText(rset.getString("FOOD_NAME"));
                FoodPrice.setText(rset.getString("FOOD_PRICE"));
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(MENU_EDIT.class.getName()).log(Level.SEVERE, null, ex);
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_FoodListValueChanged

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed

         clear();
         add = true;
         saveButton.setEnabled(true);
         FoodTypeCombo.setEnabled(true);
         resetButton.setEnabled(true);

         addButton.setEnabled(false);
         deleteButton.setEnabled(false);
         editButton.setEnabled(false);
         FoodType.setEnabled(false);

         FoodCode.setFocusable(true);
         FoodName.setFocusable(true);
         FoodPrice.setFocusable(true);

        // TODO add your handling code here:
    }//GEN-LAST:event_addButtonActionPerformed

    private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed

        clear();
        reset();
        // TODO add your handling code here:
    }//GEN-LAST:event_resetButtonActionPerformed

    private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed

        edit = true;
        saveButton.setEnabled(true);
        FoodTypeCombo.setEnabled(true);
        resetButton.setEnabled(true);

        addButton.setEnabled(false);
        deleteButton.setEnabled(false);
        editButton.setEnabled(false);
        FoodType.setEnabled(false);

        FoodName.setFocusable(true);
        FoodPrice.setFocusable(true);
        // TODO add your handling code here:
    }//GEN-LAST:event_editButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed

        String query = null;
        String a = FoodCode.getText();
        String b = FoodPrice.getText();
        String c = FoodName.getText();
        String d = (String) FoodTypeCombo.getSelectedItem();

        if(add)
        {
            String message = "Do you confirm to add?";
            String title = "Confirmation";

            int reply = JOptionPane.showConfirmDialog(null,message,title,JOptionPane.YES_NO_OPTION);

            if(reply == JOptionPane.YES_OPTION)
            {
                query = "INSERT INTO FOOD VALUES('"+a+"','"+d+"','"+c+"','"+b+"')";
                try
                {
                    stat = con.createStatement();
                    stat.executeUpdate(query);
                    JOptionPane.showMessageDialog(null, "Menu added");

                    reset();
                    clear();
                    populateData();
                }
                catch (SQLException ex)
                {
                    Logger.getLogger(MENU_EDIT.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }
        if(edit)
        {
            String message = "Do you confirm to update?";
            String title = "Confirmation";

            int reply = JOptionPane.showConfirmDialog(null,message,title,JOptionPane.YES_NO_OPTION);
            if(reply == JOptionPane.YES_OPTION)
            {
                query = "UPDATE FOOD SET FOOD_NAME = '"+c+"' , FOOD_TYPE = '"+d+"' , FOOD_PRICE = '"+b+"'"+
                    " WHERE FOOD_CODE = '"+a+"'";

                try
                {
                    stat = con.createStatement();
                    stat.executeUpdate(query);
                    JOptionPane.showMessageDialog(null, "Menu updated");

                    reset();
                    clear();
                    populateData();
                }
                catch (SQLException ex)
                {
                    Logger.getLogger(MENU_EDIT.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        // TODO add your handling code here:
    }//GEN-LAST:event_saveButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed

        String a = FoodCode.getText();
        String b = FoodPrice.getText();
        String c = FoodName.getText();
        String d = FoodType.getText();

        if(a.equals("") || b.equals("") || c.equals("") || d.equals(""))
        {
            JOptionPane.showMessageDialog(null, "No item to delete!");
        }
        else
        {
            String message = "Do you confirm to delete?";
            String title = "Confirmation";

            int reply = JOptionPane.showConfirmDialog(null,message,title,JOptionPane.YES_NO_OPTION);
            if(reply == JOptionPane.YES_OPTION)
            {
                String query = "DELETE FROM FOOD WHERE FOOD_CODE = '"+a+"' AND FOOD_NAME = '"+c+"' "+
                               "AND FOOD_PRICE = '"+b+"' AND FOOD_TYPE = '"+d+"'";
                try
                {
                    stat = con.createStatement();
                    stat.executeUpdate(query);

                    JOptionPane.showMessageDialog(null, "Menu deleted");

                    reset();
                    clear();
                    populateData();
                }
                catch (SQLException ex)
                {
                    Logger.getLogger(MENU_EDIT.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_deleteButtonActionPerformed


    private void populateData() throws SQLException
    {

        String query = "SELECT FOOD_NAME FROM FOOD ORDER BY FOOD_NAME ASC";

        List = new DefaultListModel();
        rset = getResultSet(query);
        while(rset.next())
        {
           List.addElement(rset.getString(1));
        }
        FoodList.setModel(List);
     }

    private void reset()
    {
        edit = false;
        add = false;
        saveButton.setEnabled(false);
        FoodTypeCombo.setEnabled(false);
        resetButton.setEnabled(false);

        addButton.setEnabled(true);
        deleteButton.setEnabled(true);
        editButton.setEnabled(true);
        FoodType.setEnabled(true);

        FoodCode.setFocusable(false);
        FoodType.setFocusable(false);
        FoodName.setFocusable(false);
        FoodPrice.setFocusable(false);
    }

    private ResultSet getResultSet(String s) throws SQLException {
        connect();
        ResultSet rs = null;
        try {
            stat = con.createStatement();
            rs = stat.executeQuery(s);
        } catch (SQLException ex) {
            Logger.getLogger(MENU_EDIT.class.getName()).log(Level.SEVERE, null, ex);
        }

        return rs;
    }

    private void connect() throws SQLException
    {
        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
        System.out.println("Driver Loaded");
        con = DriverManager.getConnection(url,pass_id,pass_pw);
        System.out.println("Database Connected");

    }

    private void clear()
    {
        FoodCode.setText("");
        FoodName.setText("");
        FoodPrice.setText("");
        FoodType.setText("");
        FoodTypeCombo.setSelectedIndex(0);

    }
    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new MENU_EDIT().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(MENU_EDIT.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton DoneButton;
    private javax.swing.JTextField FoodCode;
    private javax.swing.JList FoodList;
    private javax.swing.JTextField FoodName;
    private javax.swing.JTextField FoodPrice;
    private javax.swing.JTextField FoodType;
    private javax.swing.JComboBox FoodTypeCombo;
    private javax.swing.JButton addButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton editButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JButton resetButton;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables

}
